<template>
  <el-select v-model="selectedSms" clearable placeholder="请选择短信模版" :style="{ 'width': width }" @change="handleChange">
    <el-option
      v-for="item in smsList"
      :key="item.id"
      :label="item.name"
      :value="item.id"
    >
      <span style="float: left">{{ item.name }}</span>
      <span class="sms-option-text">{{ item.content }}</span>
    </el-option>
  </el-select>
</template>

<script>
/**
 * 业务组件 - 短信选择器
 */
export default {
  name: 'Sms',
  props: {
    /** 数据源 */
    value: {
      type: String,
      default: ''
    },
    /** 宽度 */
    width: {
      type: String,
      default: '120px'
    }
  },
  data() {
    return {
      api: this.$u.api.PluginsSmsData,
      smsList: [],
      selectedSms: ''
    }
  },
  watch: {
    // 监听数据
    value: {
      immediate: true,
      handler(val) {
        this.selectedSms = val
      }
    }
  },
  created() {
    // 初始化数据
    this.initData()
  },
  methods: {
    // 初始化数据
    initData() {
      this.api.template().then(res => {
        this.smsList = res.data
        console.log(this.smsList)
      })
    },
    handleChange(value) {
      this.selectedSms = value
      this.$emit('input', value)
      this.$emit('change', value)
    }
  }
}
</script>
<style lang="scss" scoped>
::v-deep{
  .el-input{
    width: 100% !important;
  }
}
.sms-option-text {
  float: right;
  color: #8492a6;
  font-size: 13px;
  width: 70%;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
</style>
